import time


class TickerClock:
    """毫秒定时器"""

    def __init__(self, diff=1000):
        self.__deadline = None
        self.__ticks = time.ticks_ms()
        self.__diff = int(diff)
        self.reset()

    def reset(self):
        self.__ticks = time.ticks_ms()
        self.__deadline = time.ticks_add(self.__ticks, self.__diff)
        #print(self.__deadline)

    def get_diff(self):
        cur_tick = time.ticks_ms()
        diff = cur_tick - self.__ticks
        return diff

    def check(self):
        diff = time.ticks_diff(self.__deadline, time.ticks_ms())
        if diff <= 0:
            self.reset()
            return True
        return False


if __name__ == "__main__":
    timer = TickerClock(2000)

    while True:
        # time.sleep_ms(100)
        # diff = timer.get_diff()
        # print(diff)
        if timer.check():
            print("do something")
